package com.beaven.imclient.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;
import com.beaven.imclient.R;
import com.beaven.imclient.adapter.MemberAdapter;
import com.beaven.imclient.base.BaseActivity;
import com.beaven.imclient.data.UserSelected;
import com.beaven.imclient.presenter.group.MemberContract;
import com.beaven.imclient.presenter.group.MemberPresenter;
import com.beaven.imclient.utils.ActivityUtil;
import com.beaven.imclient.utils.ToastUtil;
import com.beaven.imclient.widget.DividerDecoration;
import com.chad.library.adapter.base.BaseQuickAdapter;
import java.util.ArrayList;
import java.util.List;

public class GroupMemberActivity extends BaseActivity implements MemberContract.View {

  public static final String MEMBER_MANAGER = "MEMBER_MANAGER";
  public static final String GROUP_ID = "GROUP_ID";

  /**
   * 启动Activity
   *
   * @param isMemberAdd 添加群成员，或删除群成员
   * @param groupId 群ID
   */
  public static void start(Context context, boolean isMemberAdd, String groupId) {
    Intent intent = new Intent(context, GroupMemberActivity.class);
    intent.putExtra(MEMBER_MANAGER, isMemberAdd);
    intent.putExtra(GROUP_ID, groupId);
    context.startActivity(intent);
  }

  private ProgressDialog dialog;
  private List<String> memberList = new ArrayList<>();

  @Override public int getLayoutId() {
    return R.layout.activity_group_member;
  }

  private String groupId;
  private boolean isMemberAdd;
  private MemberAdapter adapter;

  private MemberContract.Presenter presenter;
  private AlertDialog alertDialog;

  @Override protected void initBefore() {
    super.initBefore();
    isMemberAdd = getIntent().getBooleanExtra(MEMBER_MANAGER, false);
    groupId = getIntent().getStringExtra(GROUP_ID);
  }

  @Override protected void initWidget() {
    super.initWidget();
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setBackgroundColor(getResources().getColor(R.color.primary));
    toolbar.setNavigationIcon(R.drawable.image_back_left);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        finish();
      }
    });
    View view_status_bar = findViewById(R.id.view_status_bar);
    view_status_bar.setBackgroundColor(getResources().getColor(R.color.primary));
    TextView text_content_center = (TextView) findViewById(R.id.text_content_center);
    text_content_center.setText(
        isMemberAdd ? getString(R.string.invite_friends) : getString(R.string.delete_group_member));
    TextView text_arrow_right = (TextView) findViewById(R.id.text_arrow_right);
    text_arrow_right.setText(getString(R.string.sure));
    text_arrow_right.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (isMemberAdd) {
          presenter.memberManager(memberList);
        } else {
          if (memberList.size() != 0) {
            showConfirm();
          }
        }
      }
    });

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_member);
    adapter = new MemberAdapter(R.layout.item_category_manager);
    recyclerView.addItemDecoration(
        new DividerDecoration(GroupMemberActivity.this, DividerDecoration.VERTICAL_LIST));
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
      @Override public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        setUserSelect(position);
      }
    });
  }

  private void showConfirm() {
    alertDialog =
        ActivityUtil.showDialog(GroupMemberActivity.this, getString(R.string.delete_group_member),
            "确认删除成员嘛", new DialogInterface.OnClickListener() {
              @Override public void onClick(DialogInterface dialog, int which) {
                presenter.memberManager(memberList);
              }
            });
    alertDialog.show();
  }

  @Override protected void initData() {
    super.initData();
    setPresenter(new MemberPresenter(this, groupId, isMemberAdd));
  }

  private void setUserSelect(int position) {
    UserSelected userSelected = adapter.getItem(position);
    if (userSelected != null) {
      if (userSelected.isSelected()) {
        memberList.remove(userSelected.getUser().getId());
      } else {
        memberList.add(userSelected.getUser().getId());
      }
      userSelected.setSelected(!userSelected.isSelected());
      adapter.setData(position, userSelected);
    }
  }

  @Override protected void onResume() {
    super.onResume();
    presenter.subscribe();
  }

  @Override protected void onPause() {
    super.onPause();
    hideLoading();
    presenter.unSubscribe();
  }

  @Override public void setPresenter(MemberContract.Presenter presenter) {
    this.presenter = presenter;
  }

  @Override public void showList(final List<UserSelected> userSelectedList) {
    runOnUiThread(new Runnable() {
      @Override public void run() {
        adapter.setNewData(userSelectedList);
      }
    });
  }

  @Override public void showLoading() {
    dialog = ProgressDialog.show(GroupMemberActivity.this, "", getString(R.string.load_now));
  }

  @Override public void hideLoading() {
    if (dialog != null) {
      dialog.dismiss();
    }
    if (alertDialog != null) {
      alertDialog.dismiss();
    }
  }

  @Override public void showText(int stringId) {
    ToastUtil.show(stringId);
  }

  @Override public void clearList() {
    memberList.clear();
  }
}
